package com.example.qxfw.dataBaseManager.nyzjk.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.CropGrowdateHistory;
import com.example.qxfw.common.entity.ny.CropGrowdateIndexlib;
import com.example.qxfw.common.entity.ny.CropyieldHistory;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.dataBaseManager.nyzjk.service.FyqclzlkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/5  星期三   16:37
 * @description : 此类用来 发育期产量资料库
 ***************************************/
@Slf4j
@Api(tags = "数据库管理-农业专家库-发育期产量资料库")
@RestController
@CrossOrigin
@RequestMapping("/FyqclzlkController")
public class FyqclzlkController {

    @Autowired
    private FyqclzlkService fyqclzlkService;

    @ApiOperation("加载站点编号")
    @GetMapping("SelectStationInfo")
    public SysResult SelectStationInfo(StationInfo stationInfo) {
        List<StationInfo> stationInfoList = this.fyqclzlkService.selectStationId(stationInfo, "stationid asc", null);
        return SysResult.success(stationInfoList);
    }

    @ApiOperation("加载农作物名称")
    @GetMapping("selectCropGrowDateIndexLib")
    public SysResult selectCropGrowDateIndexLib() {
        List<CropGrowdateIndexlib> cropnameList = this.fyqclzlkService.querySelect("cropname", null);
        return SysResult.success(cropnameList);
    }

    @ApiOperation("获取表中对应字段的信息")
    @GetMapping("selectCropGrowDateIndexLibByField")
    public SysResult selectCropGrowDateIndexLibByField(String field, String cropName) {
        List<CropGrowdateIndexlib> cropGrowdateIndexlibs = this.fyqclzlkService.querySelect(field, cropName);
        return SysResult.success(cropGrowdateIndexlibs);
    }

    @ApiOperation("获取数据")
    @GetMapping("selectData")
    public SysResult selectData(String tableName) {
        if (tableName.contains("growdate")) {
            List<CropGrowdateHistory> cropGrowdateHistories = this.fyqclzlkService.selectCropGrowdateHistory(new CropGrowdateHistory(), "stationid asc, cropname ,type,year", null);
            return SysResult.success(cropGrowdateHistories);
        } else {
            List<CropyieldHistory> cropyieldHistoryList = this.fyqclzlkService.selectCropYieldHistory(new CropyieldHistory(), "stationid asc ,cropname, type, year", null);
            return SysResult.success(cropyieldHistoryList);
        }
    }

    @ApiOperation("查询")
    @GetMapping("selectBy")
    public SysResult selectBy(String tableName) {
        if (tableName.contains("growdate")) {
            List<CropGrowdateHistory> cropGrowdateHistories = this.fyqclzlkService.selectCropGrowdateHistory(new CropGrowdateHistory(), "stationid asc", null);
            return SysResult.success(cropGrowdateHistories);
        } else {
            List<CropyieldHistory> cropyieldHistoryList = this.fyqclzlkService.selectCropYieldHistory(new CropyieldHistory(), "stationid asc", null);
            return SysResult.success(cropyieldHistoryList);
        }
    }

    @ApiOperation("判断是否存在相同记录")
    @RequestMapping("judgeRecords")
    public SysResult judgeRecords(String tableName, String stationId, String cropname, String type, String year, String growdatename, String growdatedate, String yield, String hectare) {
        CropGrowdateHistory cropGrowdateHistory = new CropGrowdateHistory();
        cropGrowdateHistory.setStationid(stationId);
        cropGrowdateHistory.setCropname(cropname);
        cropGrowdateHistory.setType(type);
        cropGrowdateHistory.setYear(Integer.parseInt(year));
        CropyieldHistory cropyieldHistory = new CropyieldHistory();
        cropyieldHistory.setStationid(stationId);
        cropyieldHistory.setCropname(cropname);
        cropyieldHistory.setType(type);
        cropyieldHistory.setYear(Integer.parseInt(year));
        Date date = null;
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            date = dateFormat.parse(growdatedate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (tableName.contains("growdate")) {
            cropGrowdateHistory.setGrowdatename(growdatename);
            cropGrowdateHistory.setGrowdatedate(date);
            List<CropGrowdateHistory> cropGrowdateHistoryList = this.fyqclzlkService.selectCropGrowdateHistory(cropGrowdateHistory, null, null);
            if (cropGrowdateHistoryList.size() > 0) {
                return SysResult.fail("已存在相同的记录");
            } else {
                return SysResult.success();
            }
        } else {
            cropyieldHistory.setYield(Integer.parseInt(yield));
            cropyieldHistory.setHectare(Integer.parseInt(hectare));
            List<CropyieldHistory> cropyieldHistoryList = this.fyqclzlkService.selectCropYieldHistory(cropyieldHistory, null, null);
            if (cropyieldHistoryList.size() > 0) {
                return SysResult.fail("已存在相同的记录");
            }
            return SysResult.success();
        }
    }

    @ApiOperation("添加发育期")
    @PostMapping("insertCropGrowDateHistory")
    public SysResult insertCropGrowDateHistory(@RequestBody CropGrowdateHistory cropGrowdateHistory) {
        Integer integer = this.fyqclzlkService.saveCropGrowdateHistory(cropGrowdateHistory);
        if (integer > 0) {
            return SysResult.success();
        }
        return SysResult.fail();
    }

    @ApiOperation("添加产量资料")
    @PostMapping("saveCropYieldHistory")
    public SysResult saveCropYieldHistory(@RequestBody CropyieldHistory cropyieldHistory) {
        Integer saveCropYieldHistory = this.fyqclzlkService.saveCropYieldHistory(cropyieldHistory);
        if (saveCropYieldHistory > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("修改发育期")
    @PostMapping("updateCropGrowDateHistory")
    public SysResult updateCropGrowDateHistory(@RequestBody CropGrowdateHistory cropGrowdateHistory) {
        Integer updateCropGrowdateHistory = this.fyqclzlkService.updateCropGrowdateHistory(cropGrowdateHistory);
        if (updateCropGrowdateHistory > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("修改产量资料")
    @PostMapping("updateCropYieldHistory")
    public SysResult updateCropYieldHistory(@RequestBody CropyieldHistory cropyieldHistory) {
        Integer updateCropYieldHistory = this.fyqclzlkService.updateCropYieldHistory(cropyieldHistory);
        if (updateCropYieldHistory > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("删除发育期")
    @GetMapping("deleteCropGrowDateHistory")
    public SysResult deleteCropGrowDateHistory(@RequestBody CropGrowdateHistory cropGrowdateHistory) {
        Integer deleteCropGrowdateHistory = this.fyqclzlkService.deleteCropGrowdateHistory(cropGrowdateHistory);
        if (deleteCropGrowdateHistory > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }

    }


}
